package com.lemon.smartsafetynew.ui.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.lemon.smartsafety.app.IApplication;
import com.lemon.smartsafetynew.util.common.CommonUtil;

/**
 * Created by Andrew on 2017/8/14.
 */

public class ProgressArcView extends View {

    private int total_degree = 270;
    private int stroke_width = 5;

    private float width = 0;
    private float height = 0;

    private Paint paintText = new Paint();
    private Paint paintBg = new Paint();
    private Paint paintProgress = new Paint();
    private RectF rectF;
    private Rect targetRect;

    private long totalSize = 0;
    private long valueSize = 0;
    private boolean showPercent = false;

    public ProgressArcView(Context context) {
        super(context);
        init();
    }

    public ProgressArcView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ProgressArcView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        if (changed) {
            width = getWidth();
            height = getHeight();
            rectF = new RectF(stroke_width, stroke_width, width - stroke_width, height - stroke_width);//确定外切矩形范围
            targetRect = new Rect(stroke_width, stroke_width, (int) (width - stroke_width), (int) (height - stroke_width));//确定外切矩形范围
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawArc(rectF, -225, total_degree, false, paintBg);//绘制圆弧，不含圆心

        Paint.FontMetricsInt fontMetrics = paintText.getFontMetricsInt();
        int baseline = (targetRect.bottom + targetRect.top - fontMetrics.bottom - fontMetrics.top) / 2;
        // 下面这行是实现水平居中，drawText对应改为传入targetRect.centerX()
        paintText.setTextAlign(Paint.Align.CENTER);
        if (showPercent) {   //show percent
            canvas.drawText(String.valueOf(valueSize * 100 / totalSize) + "%", targetRect.centerX(), baseline, paintText);
        } else {             //show value
            canvas.drawText(String.valueOf(valueSize), targetRect.centerX(), baseline, paintText);
        }

        if (totalSize != 0) {
            canvas.drawArc(rectF, -225, total_degree * valueSize / totalSize, false, paintProgress);//绘制圆弧，不含圆心
        }

    }


    /**
     * 初始化
     */
    private void init() {

        float density = getResources().getDisplayMetrics().density;

        stroke_width = (int) (density * stroke_width);

        paintText.setAntiAlias(true);   //使用抗锯齿功能
        paintText.setColor(0xFFEFF0F1);  //设置画笔的颜色
        paintText.setTextSize(CommonUtil.convertDipOrPx(IApplication.context(), 10));

        paintBg.setAntiAlias(true);   //使用抗锯齿功能
        paintBg.setColor(0xFFEFF0F1);  //设置画笔的颜色
        paintBg.setStrokeWidth(stroke_width);
        //paintBg.setStrokeCap(Paint.Cap.ROUND);
        paintBg.setStyle(Paint.Style.STROKE);//设置画笔类型为STROKE类型

        paintProgress.setAntiAlias(true);   //使用抗锯齿功能
        paintProgress.setColor(0xFFF6CA30);  //设置画笔的颜色
        paintProgress.setStrokeWidth(stroke_width);
        //paintProgress.setStrokeCap(Paint.Cap.ROUND);
        paintProgress.setStyle(Paint.Style.STROKE);//设置画笔类型为STROKE类型

    }

    /**
     * 设置progress的值
     *
     * @param total 总数
     * @param value 当前值
     */
    public void setProgress(long total, long value) {

        totalSize = total;
        valueSize = value;
        postInvalidate();
    }


    /**
     * 设置progress的值
     *
     * @param total       总数
     * @param value       当前值
     * @param showPercent true: 显示百分比
     */
    public void setProgress(long total, long value, boolean showPercent) {
        totalSize = total;
        valueSize = value;
        this.showPercent = showPercent;
        postInvalidate();
    }

    /**
     * 设置文字大小
     *
     * @param size
     */
    public void setPaintTextSize(int size) {
        paintText.setTextSize(CommonUtil.convertDipOrPx(IApplication.context(), size));
    }


}
